/**
 * Created by Administrator on 2016/8/6.
 * 处理用户登录
 */
var express = require('express');
var User = require('../db/user');
var result = require('../utils/result');
var router = express.Router();
router.get('/', function (req, res, next) {
    res.render('login');
});
router.post('/', function (req, res, next) {
    var r = result();
    if (!req.body.username || !req.body.password) {
        r.error = '用户名或密码不能为空';
        return res.json(r);
    }
    var u = {
        name: req.body.username,
        pwd: req.body.password
    };
    User.find(u, function (err, doc) {
        if (err) {
            r.error = '用户名或密码不正确';
            return;
        }
        if (doc == "") {
            r.data = "fail";
        } else {
            r.data = "success";
            req.session.userid = doc[0]._id;
            req.session.username = doc[0].name;
        }
        res.json(r.data);
    });
});
module.exports = router;